package com.iotcomm.model;

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 用户信息类
 * 存储客户端连接的相关信息
 */
public class UserInfo {
    private String username;           // 用户名
    private String channelId;          // 通道ID
    private String ipAddress;          // IP地址
    private int clientPort;            // 端口
    private LocalDateTime loginTime;   // 登录时间
    private LocalDateTime lastActiveTime; // 最后活跃时间
    private AtomicLong messageCount;   // 消息数量
    private AtomicLong trafficBytes;   // 流量字节数
    
    /**
     * 构造函数
     * @param username 用户名
     * @param channelId 通道ID
     * @param ipAddress IP地址
     */
    public UserInfo(String username, String channelId, String ipAddress, int clientPort) {
        this.username = username;
        this.channelId = channelId;
        this.ipAddress = ipAddress;
        this.clientPort = clientPort;
        this.loginTime = LocalDateTime.now();
        this.lastActiveTime = LocalDateTime.now();
        this.messageCount = new AtomicLong(0);
        this.trafficBytes = new AtomicLong(0);
    }
    
    /**
     * 更新最后活跃时间
     */
    public void updateLastActiveTime() {
        this.lastActiveTime = LocalDateTime.now();
    }
    
    /**
     * 增加消息计数
     */
    public void incrementMessageCount() {
        this.messageCount.incrementAndGet();
    }
    
    /**
     * 增加流量统计
     * @param bytes 字节数
     */
    public void addTraffic(long bytes) {
        this.trafficBytes.addAndGet(bytes);
    }
    
    /**
     * 检查是否超时（超过指定秒数未活跃）
     * @param timeoutSeconds 超时秒数
     * @return 是否超时
     */
    public boolean isTimeout(int timeoutSeconds) {
        return LocalDateTime.now().isAfter(lastActiveTime.plusSeconds(timeoutSeconds));
    }
    
    /**
     * 获取格式化的流量信息
     * @return 格式化的流量字符串
     */
    public String getFormattedTraffic() {
        long bytes = trafficBytes.get();
        if (bytes < 1024) {
            return bytes + " B";
        } else if (bytes < 1024 * 1024) {
            return String.format("%.2f KB", bytes / 1024.0);
        } else {
            return String.format("%.2f MB", bytes / (1024.0 * 1024.0));
        }
    }
    
    // Getter和Setter方法
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getChannelId() {
        return channelId;
    }
    
    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }
    
    public String getIpAddress() {
        return ipAddress;
    }
    
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }
    
    public LocalDateTime getLoginTime() {
        return loginTime;
    }
    
    public void setLoginTime(LocalDateTime loginTime) {
        this.loginTime = loginTime;
    }
    
    public LocalDateTime getLastActiveTime() {
        return lastActiveTime;
    }
    
    public void setLastActiveTime(LocalDateTime lastActiveTime) {
        this.lastActiveTime = lastActiveTime;
    }
    
    public long getMessageCount() {
        return messageCount.get();
    }
    
    public long getTrafficBytes() {
        return trafficBytes.get();
    }

    public void setClientPort(int clientPort)
    {
        this.clientPort = clientPort;
    }

    public int getClientPort()
    {
        return clientPort;
    }

    @Override
    public String toString() {
        return String.format("UserInfo{username='%s', channelId='%s', ipAddress='%s', clientPort=%d, messageCount=%d, traffic='%s'}",
                username, channelId, ipAddress, clientPort, messageCount.get(), getFormattedTraffic());
    }
}